package cn.zh.stan.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.UserItem;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.activity.AboutApp;
import cn.zh.stan.activity.Balance;
import cn.zh.stan.activity.ComInfo;
import cn.zh.stan.activity.FeedBack;
import cn.zh.stan.activity.Help;
import cn.zh.stan.activity.Login;
import cn.zh.stan.activity.MainActivity;
import cn.zh.stan.activity.PsChange;
import cn.zh.stan.activity.ReAdrass;
import cn.zh.stan.activity.UserEdit;
import cn.zh.stan.util.RoundImageView;
import it.sephiroth.android.library.picasso.Picasso;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by WYN on 2018/1/15 0015.
 */

public class MineFragment extends Fragment implements View.OnClickListener {

    Button out;
    MyApplication mapp;
    RelativeLayout useredit;
    //用户余额
    LinearLayout balance;
    Dialog dialog;
    RoundImageView mpho;
    String uid,phone;
    TextView mphon;
    UserItem userItem=null;

    LinearLayout cominfo,managrd,pschange,rebacklin,hptxtlin,aboutlin;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_mine,container,false);
        mapp= (MyApplication) getActivity().getApplication();
        userItem=mapp.getUserItem();
        uid=mapp.getSp().getString("uid","");
        phone=mapp.getSp().getString("phone","");
        initv(view);
        return view;
        }

     public void initv(View view){
         mapp.setHeadurl("");
         out=view.findViewById(R.id.out);
         useredit=view.findViewById(R.id.useredit);
         balance=view.findViewById(R.id.balance);
         mpho=view.findViewById(R.id.mpho);
         mphon=view.findViewById(R.id.mphon);
         mphon.setText(phone);
         cominfo=view.findViewById(R.id.cominfo);
         managrd=view.findViewById(R.id.managrd);
         pschange=view.findViewById(R.id.pschange);
         rebacklin=view.findViewById(R.id.rebacklin);
         hptxtlin=view.findViewById(R.id.hptxtlin);
         aboutlin=view.findViewById(R.id.aboutlin);
         out.setOnClickListener(this);
         useredit.setOnClickListener(this);
         balance.setOnClickListener(this);
         cominfo.setOnClickListener(this);
         managrd.setOnClickListener(this);
         pschange.setOnClickListener(this);
         rebacklin.setOnClickListener(this);
         hptxtlin.setOnClickListener(this);
         aboutlin.setOnClickListener(this);
         if (null==userItem){
             Picasso.with(getActivity()).load(R.mipmap.avatar).error(R.mipmap.avatar).into(mpho);
         }else {
             Picasso.with(getActivity()).load(userItem.getIndexPic()).error(R.mipmap.avatar).into(mpho);
           }
          }


    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.out:
                //退出登录
                getDialog();
                break;
            case R.id.useredit:
              Intent intent1=new Intent( getActivity(), UserEdit.class);
              startActivityForResult(intent1,0);
              break;
            case R.id.balance:
              Intent intent2=new Intent(getActivity(), Balance.class);
              startActivity(intent2);
              break;
            case R.id.ocancle:
                dialog.dismiss();
                break;
            case R.id.osure:
                SharedPreferences sp=mapp.getSp();
                SharedPreferences.Editor editor=sp.edit();
                editor.clear();
                editor.commit();
                Intent intent=new Intent(getActivity(), Login.class);
                getActivity().startActivity(intent);
                getActivity().finish();
                break;
            case R.id.cominfo:
                //公司信息
                Intent intent4=new Intent(getActivity(), ComInfo.class);
                startActivity(intent4);
                break;
            case R.id.managrd:
                //收货地址管理
                Intent intent3=new Intent(getActivity(), ReAdrass.class);
                startActivity(intent3);
                break;
            case R.id.pschange:
               //密码修改
                Intent intent5=new Intent(getActivity(),PsChange.class);
                startActivity(intent5);
                break;
            case R.id.rebacklin:
                 Intent intent6=new Intent(getActivity(), FeedBack.class);
                 startActivity(intent6);
                break;
            case R.id.hptxtlin:
                //帮助文档
                Intent intent7=new Intent(getActivity(), Help.class);
                startActivity(intent7);
                break;
            case R.id.aboutlin:
                //关于
                Intent intent8=new Intent(getActivity(), AboutApp.class);
                startActivity(intent8);
                break;
                }
             }


    @Override
    public void onResume() {
        super.onResume();
        if (mapp.getHeadurl().equals("")){
        }else {
            Bitmap bm= BitmapFactory.decodeFile(mapp.getHeadurl());
            mpho.setImageBitmap(bm);
           }

        getUinfo();
        }


    public void getDialog(){
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View v = inflater.inflate(R.layout.out_layout, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.out_layout);// 加载布局

        dialog = new Dialog(getActivity(), R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        v.findViewById(R.id.ocancle).setOnClickListener(this);
        v.findViewById(R.id.osure).setOnClickListener(this);
        }




    public void getUinfo(){
        OkHttpUtils.get(BaseUrl.men_getinfo)
                .params("userId",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js = new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                JSONObject Res=js.getJSONObject("Res");

                                String  Id=Res.getString("Id");
                                String MemberId=Res.getString("MemberId");
                                String TrueName=Res.getString("TrueName");
                                String Gender=Res.getString("Gender");
                                String  Birthday=Res.getString("Birthday");
                                String  Email=Res.getString("Email");
                                String  Phone=Res.getString("Phone");
                                String  IndexPic=Res.getString("IndexPic");
                                String  CreateTime=Res.getString("CreateTime");
                                String  IdCard1=Res.getString("IdCard1");
                                String  IdCard2=Res.getString("IdCard2");
                                String  Photo=Res.getString("Photo");
                                String  Status=Res.getString("Status");
                                String url="http://120.26.41.167:8092"+IndexPic;
                                UserItem userItem=new UserItem( Id,  MemberId,  TrueName,  Gender,  Birthday,  Email,  Phone,  url,  CreateTime,  IdCard1,  IdCard2, Photo, Status);
                                mapp.setUserItem(userItem);

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
              }
      }
